package com.lagou.service.impl;

import com.lagou.dao.AuthCodeDao;
import com.lagou.pojo.AuthCode;
import com.lagou.service.AuthCodeService;
import com.lagou.service.MailService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AuthCodeServiceImpl implements AuthCodeService {

	@Autowired
	private AuthCodeDao authCodeDao;
	@Reference
	private MailService mailService;

	@Override
	public void addAuthCode(String email) {
		//生成验证码
		int code = (int) (Math.random() * 1000000);
		String codeStr = code + "";
		//处理验证码
		AuthCode authCode = new AuthCode();
		authCode.setEmail(email);
		authCode.setCode(codeStr);
		//计算创建时间及过期时间（10分钟）
		Date createTime = new Date();
		Date expireTime = new Date(createTime.getTime() + 10 * 60 * 1000);
		authCode.setCreateTime(createTime);
		authCode.setExpireTime(expireTime);
		//持久化
		authCodeDao.save(authCode);
		//发邮件
		mailService.send(email, codeStr);
	}
}
